package seismosurfer.domain;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

/**
 * Exports data in the tsv file format.
 *
 */
public class TSVExport extends Export {

    public static final String TSV_SUFFIX = "tsv";

    public TSVExport() {
    }

    public String getExtension() {
        return TSV_SUFFIX;
    }

    public String getDescription() {
        return "TSV (Tab delimited)";
    }

    public void doExport(PrintWriter out) throws IOException {
        TSVPrinter p = new TSVPrinter(out);
        p.println(getQuakeData());
        p.close();
    }
    
    /**
     * Implements the tab-seperated values
     * file format.
     *
     */
    public class TSVPrinter {

        private Writer out;

        public TSVPrinter(Writer out) {
            this.out = out;
        }

        public void println(String values[][]) throws IOException {

            for (int i = 0; i < values.length; i++) {
                println(values[i]);
                println();
            }
            if (values.length == 0) {
                println();
            }

        }

        public void close() throws IOException {
            out.close();
        }

        public void println() throws IOException {
            out.write(System.getProperty("line.separator"));
        }

        public void println(String values[]) throws IOException {
            int i = 0;
            for (i = 0; i < values.length - 1; i++) {
                print(values[i]);
                print("\t");
            }
            print(values[i]);
        }

        public void print(String value) throws IOException {
            out.write(value);
        }

    }

}